import 'package:flutter/material.dart';
import 'package:love_app/model/community_model.dart' as CTYModel;

class SearchProvider with ChangeNotifier{
  Map<int, CTYModel.Data> _allItem = {}; // 保存社区推荐页面的所有数据

  int _page = 1; // 当前请求页数

  bool _isEndPage = false; // 是否是最后一页

  double _offset = 0.0;

  Map<int, List<String>> _showImages = {}; // 点击要显示的图片

  bool _isFirst = true; // 是否是首次加载
  
  bool _isUserPage = false; // 是否是个人动态页面

  String _searchData = ""; // 记录上次搜索的结果

  bool get isUserPage => this._isUserPage;
  set isUserPage(bool isUserPage){
    this._isUserPage = isUserPage;
  }

  bool get isFirst => this._isFirst;
  set isFirst(bool isFirst){
    this._isFirst = isFirst;
  }

  // 取值
  Map<int, CTYModel.Data> get allItem => this._allItem; // 所有的数据
  // 赋值
  int get page => this._page;
  set page(int page){
    this._page = page;
  }

  bool get isEndPage => this._isEndPage;
  set isEndPage(bool isEndPage){
    this._isEndPage = isEndPage;
  }

  String get searchData => this._searchData;
  set searchData(String searchData){
    this._searchData = searchData;
  }

  double get offset => this._offset;
  set offset(double offset){
    this._offset = offset;
  }

  // 给allItem赋值
  setAllItem(int itemID, CTYModel.Data data){
    this._allItem[itemID] = data;
    List<String> imageList = [];
    data.pictures.forEach((item){
      imageList.add(item.origin);
    });
    this._showImages[itemID] = imageList;
  }

  // 获取
  List<String> showImages(int itemID){
    return this._showImages[itemID];
  }

  // 获取所有的item
  List<CTYModel.Data> getAllItem(){
    return this._allItem.values.toList();
  }

  // 通过id获取item数据
  CTYModel.Data getItemById(itemId){
    return this._allItem[itemId];
  }

  // 更新点赞的值, 并更新羡慕人数
  void updateAgreeData(int itemId){
    CTYModel.Data item = this._allItem[itemId];
    if(item.isAgree == 1){
      item.isAgree = 0;
      item.agree --;
    }else{
      item.isAgree = 1;
      item.agree ++;
    }
    notifyListeners();
  }

  // 根据itemID删除文章
  void deleteItemById(itemId){
    this._allItem.remove(itemId);
  }

  // 评论值更新
  void updateCollectionsData(itemId){
    CTYModel.Data item = this._allItem[itemId];
    item.collections ++;
    notifyListeners();
  }

  // 修改关注状态
  void updateFollowingById(itemId, int followingData){
    this._allItem[itemId].isFollowing = followingData;
  }

  void clean(){
    this._allItem.clear();
    this._isEndPage = false;
    this._isFirst = true;
    this._offset = 0.0;
    this._page = 1;
    this._showImages.clear();
  }
}